var app = getApp(),
  a = app.requirejs("core"),
  b = app.requirejs("api/kjb");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //选中点的颜色 
    indicatorDots: true,
    //是否自动切换
    autoplay: true,
    //自动切换的间隔
    interval: 5000,
    //滑动动画时长毫秒
    duration: 1000,
    //所有图片的高度
    imgheights: 0,
    //图片宽度
    imgwidth: 750,
    //默认
    current: 0,
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    countDownDay: 0,
    countDownHour: 0,
    countDownMinute: 0,
    countDownSecond: 0,
    list: [],
    show_time: true,
    display: false,
    isOn: false,
    n_animationData: '',
    news_num: 0, //未读消息
    get_phone: false,
    show_rec: 0, // 是否显示海报推荐人
    show_chat: 0, // 是否显示聊天
  },
  imageLoad: function (e) {
    var winWid = wx.getSystemInfoSync().windowWidth; //获取当前屏幕的宽度
    var imgh = e.detail.height;//图片高度
    console.log(imgh,"aaa")
    var imgw = e.detail.width;//图片宽度
    var swiperH = winWid * imgh / imgw + "px"
    console.log(swiperH,"bbb")
    this.setData({
      imgheights: swiperH//设置高度
    })
    
  },

  onGotUserInfo: function (q) {
    var that = this,
        cache = app.getCache("userinfo");

    // if (cache) {
    //   return;
    // }
    app.getUserInfo(q.detail.userInfo, function (t) {
      console.log("t",t);
      if (t != 1000) {
        var ucache = getApp().getCache("userinfo");
        if (ucache) {
          //getCurrentPages()[getCurrentPages().length - 1].onLoad();
          that.detail()
          that.goodslist()
          that.setData({
            display: false,
            getPhoneAuth: getApp().globalData.getphone
          })
        }
      } else {
        console.log("fail",t);
        that.setData({
          display: true
        })
      }

    }, q.detail.encryptedData, q.detail.iv);

  },

  // 授权获取用户手机号
  getMobile: function (e) {
    app.globalData.getphone = false;
    this.setData({
      getPhoneAuth: false
    })
    app.getPhoneNumber(e, this, 3);
  },

  // 授权获取用户信息判断
  authCheck: function () {
    var cache = getApp().getCache("userinfo");
    if (!cache.nickName && !cache.avatarUrl) {
      this.setData({
        display: true
      })
      return false;
    } else {
      this.setData({
        display: false
      })
      return true;
    }
  },

  // 取消登录
  cancel_auth: function () {
    this.setData({
      display: false
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var cache = app.getCache("userinfo");
    var that = this;
    if (!cache.nickName && !cache.avatarUrl) {
      getApp().authSwitch(function(res){
        if (res == 1) {
          that.setData({
            display: true
          })
        } else {
          that.setData({
            display: false
          })
        }
      })
    }

    if (options.scene != undefined) {
      var scene = decodeURIComponent(options.scene);
      scene = scene.split(',');
      options.id = scene[0];
      options.pid = scene[1];
      options.card_id = scene[2];

      if (options.card_id != undefined) {
        app.globalData.card_id = options.card_id;
      }
    } else {
      if (options.card_id != undefined) {
        app.globalData.card_id = options.card_id;
      }
    }
    console.log("options",options)

    a.setting();
    this.setData(options)
    this.detail()
    this.goodslist()
    app.get_news_num(this);
  },

  onShow: function () {
    this.hideModal();
  },

  to_chat: function() {
    a.jump('/yb_mingpian/pages/chats/chats');
  },

  // 聊天时获取用户手机号
  getPhoneNumber: function(e) {
    app.getPhoneNumber(e,this,1);
  },

  /**
   * 商品详情
   */
  detail: function () {
    var that = this,
        id = that.data.id;

    app.userlog(6, '砍价商品', 'bargain_browse', id);

    b.kj_detail(id, that, function (t) {
      that.setData(t)
      console.log("bargain",t)
      //倒计时
      if (t.bargain_info.end_time) {
        var time = t.bargain_info.end_time;
        wx.setNavigationBarTitle({
          title: t.bargain_info.bargain_name || "活动详情"
        })
        b.Countdown(time, function (i) {
          that.setData(i)
        })
      }
    })
  },
  goodslist: function () {
    var that = this;
    b.kj_list('', 1, 1, that, function (t) {
      that.setData(t)
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({ list: [] });
    this.detail();
    this.goodslist();
    wx.stopPullDownRefresh();
  },
  /**
   * 跳转至地图
   */
  navigate: function () {
    var that = this;
    var t = this.data.about_info;
    if (t.name && t.lat && t.lng) {
      a.tx_map(t.lat, t.lng, t.name)
    } else {
      a.toast('获取位置失败')
    }
  },
  /**
 * 跳转至详情
 */
  url: function (e) {
    var data = a.pdata(e);
    wx.navigateTo({
      url: '/yb_mingpian/pages/kanjia/goods_info/index?id=' + data.id
    })
  },
  /**
   * 直接购
   */
  shoping: function (e) {
    var auth_res = this.authCheck();
    if (!auth_res) {
      return;
    }

    var that = this,
      data = a.pdata(e);
    if (!that.data.show_time) {
      a.alert('该活动已经结束')
      return false;
    }
    if (that.data.bargain_info.bargain_inventory < 1) {
      a.alert('库存不足')
      return false;
    }
    wx.navigateTo({
      url: "/yb_mingpian/pages/kanjia/order/create/index?bargain_id=" + that.data.bargain_info.id + "&total=1&uid=" + getApp().getCache("userinfo").uid + "&activity_order_type=0&current_price=" + that.data.bargain_info.original_price
    })
  },
  /**
   * 发起砍
   */
  formSubmit: function (e) {
    var auth_res = this.authCheck();
    if (!auth_res) {
      return;
    }

    var that = this,
      form_id = e.detail.formId,
      bargain_id = e.detail.value.id,
      data = a.pdata(e);
    if (!that.data.show_time) {
      a.alert('该活动已经结束')
      return false;
    }
    if (that.data.bargain_info.bargain_inventory < 1) {
      a.alert('库存不足')
      return false;
    }
    // a.get('Bargain/Push', {
    //   bargain_id: bargain_id,
    //   user_id: getApp().getCache("userinfo").uid,
    //   form_id: form_id
    // }, function (t) {
    //   console.log(t)
      wx.navigateTo({
        url: '/yb_mingpian/pages/kanjia/discount_info/index?id=' + bargain_id + '&form_id=' + form_id
      })
    // })
    
  },
  /**
   * 打电话
   */
  phone: function (e) {
    a.phone(e)
  },

  /**
   * 回到名片页
   */
  toCard: function () {
    a.jump('/yb_mingpian/pages/card/index', 3);
  },

  // 底部弹窗设置
  show_Modal: function () {
    var that = this;

    a.get("poster/getConf", {
          type: 2
        }, function (res) {
          if (res.code == 0) {
            that.setData({
              show_rec: res.info.is_set
            })
          }

          var cache = getApp().getCache('userinfo');
          if (!cache.nickName && !cache.avatarUrl) {
            that.setData({
              show_rec: false
            })
          }

          // 显示遮罩层
          var animation = wx.createAnimation({
            duration: 200,
            timingFunction: "ease-in-out",
            delay: 0
          })
          that.animation = animation
          animation.translateY(500).step()
          that.setData({
            animationData: animation.export(),
            isOn: true
          })
          setTimeout(function () {
            animation.translateY(0).step()
            that.setData({
              animationData: animation.export()
            })
          }.bind(that), 200)
    })

  },

  hideModal: function () {
    this.setData({
      isOn: false,
    })
  },
  //转发
  onShareAppMessage: function (e) {
    this.hideModal();
    app.userlog(2, '砍价商品', 'bargain_relay');

    var user_name = getApp().getCache('userinfo').nickName != undefined ? getApp().getCache('userinfo').nickName : '';

    var that = this.data;
    return {
      title: user_name + "邀请您帮忙砍价，最低可以砍到" + that.bargain_info.lowest_price + "元",
      //path: '/yb_mingpian/pages/kanjia/share_info/index?id=' + that.bargain_info.id ,
      path: '/yb_mingpian/pages/kanjia/goods_info/index?id=' + that.bargain_info.id + '&card_id=' + app.globalData.card_id,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
})